//
//  InputView.swift
//  Exchange
//
//  Created by rui on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum InputStyle {
    case none
    case title
    case imageVerify
}

@objc protocol InputViewDelegate: NSObjectProtocol {
    @objc optional func inputViewTextDidChange(inputView: InputView)
    @objc optional func inputViewImageVerifyClick(inputView: InputView)
    @objc optional func inputViewTitleClick(inputView: InputView)
}

class InputView: UIView,UITextFieldDelegate {
    
    
    @IBInspectable var fontSize: CGFloat = 15 {
        didSet {
            reLayout()
        }
    }
    
    var placeholder: String? {
        get {
            return self.input.placeholder
        }
        set {
            self.input.placeholder = newValue
            if ThemeManager.default.currentThemeMode() == .night {
                input.setValue(UIColor(hexStr: "#616979"), forKeyPath: "_placeholderLabel.textColor")
            }else {
                input.setValue(UIColor(hexStr: "#b1b2b2"), forKeyPath: "_placeholderLabel.textColor")
            }
        }
    }
    
    var inputTintColor: UIColor! {
        get {
            return self.input.tintColor
        }
        set {
            self.input.tintColor = newValue
        }
    }
    
    var style: InputStyle = .none {
        didSet {
            reLayout()
        }
    }
    
    weak var delegate: InputViewDelegate?
    
    lazy var input: UITextField = {
        let input = UITextField()
        input.font = UIFont.systemFont(ofSize: fontSize)
        input.delegate = self
        input.tintColor = kMouseColor
        input.themeTextColor = ThemeManager.ThemeColorKeys.text2
        input.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .allEditingEvents)
        return input
    }()
    
    private lazy var underLine: UIView = {
        let underLine = UIView()
        if ThemeManager.default.currentThemeMode() == .day {
            underLine.backgroundColor = kGrayColor
        }else {
            underLine.backgroundColor =  kHighGrayColor
        }
        return underLine
    }()
    
    @IBInspectable var title: String? {
        get {
            return self.titleLabel?.title(for: .normal)
        }
        set {
            self.titleLabel?.setTitle(newValue, for: .normal)
            setNeedsLayout()
        }
    }
    
    @IBInspectable var image: UIImage? {
        get {
            return self.titleLabel?.image(for: .normal)
        }
        set {
            self.titleLabel?.setImage(newValue, for: .normal)
            setNeedsLayout()
            setNeedsDisplay()
        }
    }
    
    private lazy var titleLabel: UIButton? = {
        let titel = UIButton(type: .custom)
        titel.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        titel.themeSetTitleColor(ThemeManager.ThemeColorKeys.text2, for: .normal)
        titel.addTarget(self, action: #selector(titleClick), for: .touchUpInside)
        self.addSubview(titel)
        return titel
    }()
    
    @IBInspectable var imageCode: UIImage? {
        get {
            return self.imageCodeBtn?.image(for: .normal)
        }
        set {
            self.imageCodeBtn?.setImage(newValue, for: .normal)
            setNeedsLayout()
            setNeedsDisplay()
        }
    }
    
    lazy var imageCodeBtn: UIButton? = {
        let btn = UIButton(type: .custom)
        btn.addTarget(self, action: #selector(imageCodeClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var titleLeftLine: UIView? = {
        let leftLine = UIView()
        leftLine.themeBackground = ThemeManager.ThemeColorKeys.separator
        return leftLine
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initUI()
    }
    
    func initUI() {
        self.addSubview(input)
        self.addSubview(underLine)
        switch style {
        case .none:
            dPrint("")
            input.clearButtonMode = .whileEditing
        case .imageVerify:
            self.addSubview(self.imageCodeBtn!)
        case .title:
            self.addSubview(titleLabel!)
            self.addSubview(titleLeftLine!)
        }
    }
    func reLayout() {
        if style == .title {
            if !self.subviews.contains(titleLabel!) {
                self.addSubview(titleLabel!)
                self.addSubview(titleLeftLine!)
            }
            self.titleLabel?.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        }else if style == .imageVerify {
            if !self.subviews.contains(imageCodeBtn!) {
                self.addSubview(self.imageCodeBtn!)
            }
        }
        self.input.font = UIFont.systemFont(ofSize: fontSize)
        self.setNeedsLayout()
        self.layoutIfNeeded()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.input.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height - 1)
        self.underLine.frame = CGRect(x: 0, y: self.height - 0.5, width: self.width, height: 0.5)
        if style == .title {
            self.titleLabel?.sizeToFit()
            let w:CGFloat = (self.titleLabel?.frame.size.width)! + 10
            self.titleLabel?.width = w
            self.titleLabel?.x = 0
            self.titleLabel?.centerY = self.height * 0.5
            
            self.titleLeftLine?.width = 0.5
            self.titleLeftLine?.height = 15
            self.titleLeftLine?.centerY = self.height * 0.5
            self.titleLeftLine?.x = (self.titleLabel?.right)! + 5
            self.input.width = self.width - self.titleLeftLine!.x - 10
            self.input.x = self.titleLeftLine!.x + 5
        }else if style == .imageVerify {
            self.input.width = self.width - 100
            self.imageCodeBtn?.frame = CGRect(x: self.input.right, y: 0, width: 100, height: self.height)
        }
    }
    
    @objc private func imageCodeClick(){
        self.delegate?.inputViewImageVerifyClick?(inputView: self)
    }
    
    @objc private func titleClick() {
        self.delegate?.inputViewTitleClick?(inputView: self)
    }
    // MARK: - UITextFieldDelegate
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if ThemeManager.default.currentThemeMode() == .night {
            underLine.backgroundColor = kGrayColor
        }else {
            underLine.backgroundColor =  kHighGrayColor
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if ThemeManager.default.currentThemeMode() == .day {
            underLine.backgroundColor = kGrayColor
        }else {
            underLine.backgroundColor =  kHighGrayColor
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == " " {
            return false
        }
        return true
    }
    
    @objc func textFieldDidChange(_ textField: UITextField)  {
        self.delegate?.inputViewTextDidChange?(inputView: self)
    }
}
